package com.avensen.plc;

import redis.clients.jedis.Jedis;

public class CustomJedis {
    private static Jedis jedis = null;
    public static Jedis getInstance() {
        if(jedis == null){
            Jedis jedisT = new Jedis("8.134.118.190",6379);
            //授权
            jedisT.auth("PJ20210324");
            //改用我自己的，8.134.118.190的redis速度慢
//            Jedis jedisT = new Jedis("119.91.64.83",6379);
//            //授权
//            jedisT.auth("123456");
            //输出PONG，redis连通成功
            System.out.println(jedisT.ping());
            String ret = jedisT.ping();
            if(ret.equals("PONG")){
                jedis  = jedisT;
                return jedis;
            }
        }
        return jedis;
    }
    public static void initPLCToSWRedisKey(){
        String keyPre = "PLCSW:DT";
        String key = "";
        for(int i = 20100; i < 20150; i++){
            key = keyPre + i;
            getInstance().set(key,"0");
        }
        for(int i = 30100; i < 30150; i++){
            key = keyPre + i;
            getInstance().set(key,"0");
        }
        for(int i = 32000; i < 32020; i++){
            key = keyPre + i;
            getInstance().set(key,"0");
        }
    }
    public static void initSWToPLCRedisKey(){
        String keyPre = "SWPLC:DT";
        String key = "";
        for(int i = 30350; i < 30500; i++){
            key = keyPre + i;
            getInstance().set(key,"0");
        }
        for(int i = 32000; i < 32020; i++){
            key = keyPre + i;
            getInstance().set(key,"0");
        }
    }

    public static void main(String[] args) {
        initPLCToSWRedisKey();
        initSWToPLCRedisKey();
    }

    public static String getHexString(String key){
        if(!getInstance().exists(key)){
            getInstance().set(key,"0");
        }
        System.out.println("key:"+key);
        String keyvalue = getInstance().get(key);
        System.out.println("keyvalue:"+keyvalue);
        //手动写入redis的值可能是10进制的，所以这里做一下转 换
        System.out.println("key:"+key+",value:"+keyvalue);
        keyvalue = keyvalue == null ? "0" : keyvalue;
        keyvalue = keyvalue.trim();
        System.out.println("key:"+key+",value:"+keyvalue);
        if(keyvalue.length() < 4) {
            Integer value = (Integer.valueOf(keyvalue) << 8 ) & 0xffff;
            keyvalue =  Integer.toHexString(value);
        }
//        System.out.println("key:"+key+",value:"+keyvalue);
        String hexStr = String.format("%4s", keyvalue).replace(' ', '0');
//        System.out.println("hexStr:"+hexStr);
        return hexStr;
    }
    public static String getHexStringForOneByte(String key){
        if(!getInstance().exists(key)){
            getInstance().set(key,"0");
        }
        System.out.println("key:"+key);
        String keyvalue = getInstance().get(key);
        System.out.println("keyvalue:"+keyvalue);
        //手动写入redis的值可能是10进制的，所以这里做一下转 换
        System.out.println("key:"+key+",value:"+keyvalue);
        keyvalue = keyvalue == null ? "0" : keyvalue;
        keyvalue = keyvalue.trim();
        System.out.println("key:"+key+",value:"+keyvalue);
        return keyvalue;
    }
}
